//
//  JMWheelView.m
//  VoluntaryStar
//
//  Created by ios on 15/8/23.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMWheelView.h"
#import "UIViewExt.h"
#import "JMHeader.h"

@interface JMWheelView (){

    UIImage *image;
}

@property (nonatomic,strong) UIImageView *whellView;
@property (nonatomic,strong) UIButton *btn;


@end

@implementation JMWheelView


-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self addSubview:self.whellView];
        image = [UIImage imageNamed:@"ship.bundle/main_Rudder_image"];
    }
    return self;
}
-(UIImageView *)whellView{
    if(!_whellView){
        _whellView = [UIImageView new];
        _whellView.backgroundColor = [UIColor clearColor];
        if(iphone4){
            _whellView.frame = CGRectMake(self.frameWidth * 0.15/2.0, self.frameWidth * 0.15/2.0, self.frameWidth * 0.85, self.frameHeight *0.85);
        }else{
            _whellView.frame = CGRectMake(0, 0, self.frameWidth, self.frameHeight);
        }
    }
    return _whellView;
}
-(UIImage *)wheelImage{
    return image;
}

-(void)setWheelImage:(UIImage *)wheelImage{
    if(![image isEqual:wheelImage]){
        image = wheelImage;
        [self layoutIfNeeded];
    }
}
-(UIButton *)btn{
    if(!_btn){
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_btn setTitle:@"我" forState:UIControlStateNormal];
        [_btn setImage:[UIImage imageNamed:@"ship.bundle/button_person"] forState:UIControlStateNormal];
        _btn.titleLabel.font = [UIFont systemFontOfSize:UI_SCREEN_WITH *0.053125];
        _btn.backgroundColor = [UIColor clearColor];
        _btn.frame = CGRectMake(self.whellView.center.x - 35 ,self.frameHeight *0.38307 , 70, 70);
        [_btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}

#pragma mark  layoutSubView;
-(void)layoutSubviews{
    [self.whellView setImage:image];
    [self addSubview:self.btn];
}
-(void)buttonClick:(UIButton *)sender{
    if([self.delegate respondsToSelector:@selector(centerButtonClick:)]){
        [self.delegate centerButtonClick:self];
    }else{
         @throw [NSException exceptionWithName:NSExpansionAttributeName reason:@"wheel没有实现代理方法" userInfo:nil];
    }
}
static float delaAngle;
#pragma mark 重写一些方法
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint point = [touch locationInView:self];
    float dx = point.x - self.whellView.center.x;
    float dy  = point.y -self.whellView.center.y;
    delaAngle = atan2(dy, dx);
    _startTransfrom = self.whellView.transform;
    return YES;
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint point = [touch locationInView:self];
    CGFloat dx = point.x - self.whellView.center.x;
    CGFloat dy  = point.y -self.whellView.center.y;
    CGFloat ang = atan2(dy, dx);
    
    
    CGFloat angDifferent = ang - delaAngle;
    self.whellView.transform = CGAffineTransformRotate(_startTransfrom, angDifferent);
    
    CGFloat radius = atan2f(self.whellView.transform.b, self.whellView.transform.a);
    CGFloat degree = radius * (180 / M_PI);
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:[NSString stringWithFormat:@"%lf",degree] forKey:@"angDifferent"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"WHELLROTATION" object:dict];
    return YES;
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{

    CGFloat radians = atan2f(self.whellView.transform.b, self.whellView.transform.a);
    signed int  degree = radians * (180 / M_PI);
    signed int i;
    if(degree > 0){
        if(degree%60 > 30){
            i = (degree/60 +1) * 60;
        }else{
            i = (degree/60) *60;
        }
    }else{
        if(degree%60 > 30){
            i = (degree/60 -1) * 60;
        }else{
            i = (degree/60) *60;
        }
    }
    
//        float tt = i/M_PI;
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:[NSString stringWithFormat:@"%d",i] forKey:@"angDifferent"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"WHELLROTATIONEND" object:dict];
    
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDuration:0.2];
//    CGAffineTransform t = CGAffineTransformRotate(self.whellView.transform, tt);
//    self.whellView.transform = t;
//    [UIView commitAnimations];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
